import ajax from '../../../js/ajax.js';

let bm = document.getElementById('bm');
let yg = document.getElementById('yg');

let bmsz = [];
let ygsz = [];

function bmHQ() {
  ajax.send('/linkinfo/queryAllDept', {}, (data) => {
    if (data.success) {
      bmsz = data.list;
      showBM();
      return;
    }
    alert(data.message);
  });
}

function showBM() {
  console.log('部门列表', bmsz);
  for (let i = 0; i < bmsz.length; i++) {
    let p = bmsz[i];
    //生成option放到省份信息
    let op = document.createElement('option');
    op.setAttribute('value', p.deptId);
    op.append(p.deptName);
    bm.append(op);
  }
  if (bmsz.length > 0) {
    bm.selectedIndex = parseInt(bmsz.length / 2);
    //数据变化触发联动
    ygHQ();
  }
}

function ygHQ() {
  let deptId = bm.value;
  ajax.send('/linkinfo/queryEmployeeByDept', { deptId: deptId }, (data) => {
    if (data.success) {
      ygsz = data.list;
      showYG();
      return;
    }
    alert(data.message);
  });
}

function showYG() {
  yg.innerHTML = '';
  for (let i = 0; i < ygsz.length; i++) {
    let y = ygsz[i];
    let op = document.createElement('option');
    op.setAttribute('value', y.employeeId);
    op.append(y.employeeName);
    yg.append(op);
  }
  if (ygsz.length > 0) {
    yg.selectedIndex = parseInt(ygsz.length / 2);
  }
}

bm.addEventListener('change', ygHQ);
bmHQ();

let Ok=document.getElementById('Ok');
let Info=document.getElementById('Info');

Ok.addEventListener('click',()=>{
  Info.innerHTML='';
  let pindex=bm.selectedIndex;
  if(pindex>-1){
    let b=bmsz[pindex];
    Info.append(b.deptName);
  }

  let cindex=yg.selectedIndex;
  if(cindex>-1){
    let y=ygsz[cindex];
    Info.append(y.employeeName);
  }
});
